﻿// -----------------------------------------------------------------------
// <copyright file="WebPartRenderControlAdapter.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace LifeInSharePoint.Blog.Common.WebPartAdapter
{

    using System.Web.UI;
    using System.Web.UI.WebControls.WebParts;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class WebPartRenderControlAdapter : System.Web.UI.Adapters.ControlAdapter
    {

        protected override void Render(HtmlTextWriter writer)
        {

            bool inEditMode = false;  

            WebPartZone wpz = Control as WebPartZone;
            if (wpz != null &&
                    wpz.ID != "wpz")
            {
                SPWebPartManager swpm = (SPWebPartManager)SPWebPartManager.GetCurrentWebPartManager(wpz.Page);
                inEditMode = !swpm.GetDisplayMode().AllowPageDesign;
            }
            if (inEditMode && wpz.DisplayTitle.Contains("Clean"))
            {
                // Render the WebPartZone
                writer.Indent++;
                // Render the web parts
                if (wpz.WebParts.Count > 0)
                {
                    WebPartCollection wpColl = new WebPartCollection(wpz.WebParts);

                    foreach (WebPart wp in wpColl)
                    {
                        wp.RenderControl(writer);
                    }
                }
                writer.Indent--;

                writer.WriteLine();
            }
            else
            {
                // If we are editing the page --> render the web part as usual.
                base.Render(writer);
            }
        }

    }
}

